package com.shiwaixiangcun.customer.module.houserenting

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import com.google.gson.reflect.TypeToken
import com.jaeger.recyclerviewdivider.RecyclerViewDivider
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.shiwaixiangcun.customer.ConfigSps
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.GlobalConfig
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.adapter.AdapterRenting
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.entity.CommonObjCallBack
import com.shiwaixiangcun.customer.network.ResponseConfig
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr
import com.shiwaixiangcun.customer.utils.JsonUtil
import com.shiwaixiangcun.customer.widget.CustomLoadMoreView
import kotlinx.android.synthetic.main.activity_house_list.*
import kotlinx.android.synthetic.main.top_bar_white.*


/**
 * 房屋列表
 */
class HouseListActivity : BaseActivity() {

    var mHouseList: ArrayList<HouseData>? = null
    var mAdapterRenting: AdapterRenting? = null

    var siteId: Int = 20
    private var pageStart: Int = 1
    var pageSize: Int = 15
    var type: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_house_list)
        siteId = AppSharePreferenceMgr.get(mContext, ConfigSps.CURRENT_SITE_ID, 20) as Int
        pageStart = GlobalConfig.first_page
        pageSize = GlobalConfig.page_size

        iniBundleData()
        initView()

        initData(false)


    }

    private fun iniBundleData() {
        type = intent.extras.getString("type")


    }


    /**
     * 请求数据
     */
    private fun initData(isLoadMore: Boolean) {
        OkGo.get<String>(GlobalApi.houseRenting)
                .params("page.pn", pageStart)
                .params("page.size", pageSize)
                .params("siteId", siteId)
                .params("typeValue", type)
                .execute(object : StringCallback() {
                    override fun onError(response: Response<String>?) {
                        super.onError(response)
                        Log.e(BUG_TAG, "error")
                        mAdapterRenting?.setEnableLoadMore(true)
                        mAdapterRenting?.loadMoreFail()
                        refreshLayout.finishRefresh()
                    }

                    override fun onSuccess(response: Response<String>?) {
                        val type = object : TypeToken<CommonObjCallBack<HouseList>>() {}.type

                        val responseData = JsonUtil.fromJson<CommonObjCallBack<HouseList>>(response?.body(), type)
                        when (responseData.responseCode) {
                            ResponseConfig.SUCCESS -> {
                                val size = responseData.data.elements.size
                                if (isLoadMore) {
                                    if (size > 0) {
                                        mHouseList?.addAll(responseData.data.elements)
                                        mAdapterRenting?.notifyDataSetChanged()
                                    }
                                    if (size == 0 || size < pageSize) {
                                        mAdapterRenting?.loadMoreEnd(false)
                                    } else {
                                        mAdapterRenting?.loadMoreComplete()
                                    }
                                } else {
                                    refreshLayout.finishRefresh()
                                    mAdapterRenting?.setEnableLoadMore(true)
                                    if (size == 0) {
                                        clayout_nodata.visibility = View.VISIBLE
                                    } else {
                                        mHouseList?.clear()
                                        mHouseList?.addAll(responseData.data.elements)
                                        mAdapterRenting?.notifyDataSetChanged()
                                    }
                                }
                            }
                        }

                    }
                })
    }

    /**
     * 初始化试图
     */
    private fun initView() {

        if (type.equals("rentHouse")) {
            tv_page_name.text = "租房"
        } else {
            tv_page_name.text = "二手房"
        }

        back_left.setOnClickListener { finish() }
        mHouseList = ArrayList()
        mAdapterRenting = AdapterRenting(mHouseList)

        rv_house_list.layoutManager = LinearLayoutManager(this)
        rv_house_list.adapter = mAdapterRenting
        val divider = RecyclerViewDivider.Builder(mContext)
                .setOrientation(RecyclerViewDivider.VERTICAL)
                .setStyle(RecyclerViewDivider.Style.BETWEEN)
                .setMarginLeft(20f)
                .setMarginRight(20f)
                .setEndSkipCount(1)
                .setSize(0.5f)
                .setColorRes(R.color.color_divider_0_1)
                .build()
        rv_house_list.addItemDecoration(divider)

        mAdapterRenting?.setOnItemClickListener { adapter, view, position ->
            val houseData = adapter.data[position] as HouseData
            val bundle = Bundle()
            bundle.putLong("houseId", houseData.id)
            readyGo(HouseDetailActivity::class.java, bundle)
        }

        mAdapterRenting?.setLoadMoreView(CustomLoadMoreView())
        mAdapterRenting?.setOnLoadMoreListener({
            pageStart += 1
            refreshLayout.finishRefresh()
            initData(true)
        }, rv_house_list)

        refreshLayout.setOnClickListener {
            pageStart = 1
            mAdapterRenting?.setEnableLoadMore(false)
            initData(false)
        }


    }


}



